package flextv.core.logic {

import flash.filesystem.File;
import flash.utils.describeType;

import flextv.core.vo.IItem;
import flextv.core.vo.Playlist;
import flextv.core.vo.StoredItem;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.logging.ILogger;
import mx.logging.Log;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
	

public class Util {
	
	
	private static const LOG: ILogger = Log.getLogger("flextv.core.logic.Util");
	
	
	public function Util() {
		throw new Error("Instantiation prohibited!");
	}
	
	
	
	public static const DEFAULT_FAULT_FUNCTION: Function = function(data: Object): void {
		LOG.warn(ObjectUtil.toString(data));
		
		var msg: String = "Some internal error occured!";
		if(data is FaultEvent) {
			msg = (data as FaultEvent).fault.message;
		}
		msg = msg + "\n(See logfile for more details)";
		
		Alert.show(msg, "Error");
	};
	
	public static function getParentPlaylist(storedItem: StoredItem): Playlist {
		var parentPlaylist: Playlist = null;
		
		for each(var p: Playlist in CoreModel.instance.playlists) {
			if(p.id == storedItem.playlistId) {
				parentPlaylist = p;
				break;
			}
		}
		if(parentPlaylist == null) {
			throw new Error("Could not determine parent playlist of item: " + storedItem);
		}
		
		return parentPlaylist;
	}

	/**
	 * @return eg: File("/Users/xxx/Library/Preferences/FlexTV/Local Store/videos/alWAwUQxTbc.flv")
	 */
	public static function resolveFlvFile(item: IItem): File {
		if(File.applicationStorageDirectory.exists == false) {
			File.applicationStorageDirectory.createDirectory();
		}
		// return File.applicationStorageDirectory.resolvePath("videos" + File.separator + yid + ".flv");
		return new File(File.applicationStorageDirectory.nativePath + File.separator + "videos" + File.separator + item.yid + ".flv");
	}
	
	public static function resolveThumbFile(item: IItem): File {
		if(File.applicationStorageDirectory.exists == false) {
			File.applicationStorageDirectory.createDirectory();
		}
		return new File(File.applicationStorageDirectory.nativePath + File.separator + "thumbnails" + File.separator + item.yid + ".jpg");
	}
	
	
	public static function formatSeconds(inputSeconds: Number): String {
		var min: uint = Math.floor(inputSeconds / 60);
		var sec: uint = Math.floor(inputSeconds % 60);
		// StringUtil.substitute("{0}:{1}", minString, secString);
		return (min < 10 ? "0" : "") + min + ":" +
		       (sec < 10 ? "0" : "") + sec;
	}
	
	public static function getFullQualifiedClassName(clazz: Class): String {
		const type: XML = describeType(clazz);
        const className: String = type.@name;
        return className.replace("::", ".");
	}
	
	public static function randInt(startInclusive: int, endExclusive: int): int {
		return Math.round(Math.random() * (endExclusive - startInclusive)) + startInclusive;
	}
	
	public static function randElement(array: Array): Object {
		return array[Util.randInt(0, array.length)];
	}
	
	public static function randElements(array: Array, length: uint): Object {
		var result: String = "";
		const n: int = array.length - 1;
		for(var i: int=0; i < length; i++) {
			const idx: int = Util.randInt(0, n);
			result += array[idx];
		}
		return result;
	}
	
	
	/**
	 * parses XML result from youtube data api
	 */
	public static function extractFirstThumbnail(entry: Object): String {
		var thumbnails: ArrayCollection = entry.group.thumbnail as ArrayCollection;
		var tn: Object = thumbnails.getItemAt(0);
		return tn.url as String;
	}

}
}